package function;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.JdbcUtil;

public class Cost_add 
{
	private Connection conn = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
	public Cost_add()
	{
		String table = "cost";
		String sql = "insert into "+table+" values(?,?,?,now(),?)";
		
		conn = JdbcUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public int add(String cout,int direct,double money)
	{
		int isInsert = 0;
		int index = 1;
		try {
			rs = conn.createStatement().executeQuery("select inde from cost");
			while(rs.next())
			{
				index = rs.getInt(1)+1;
			}
			ps.setString(1,cout);
			ps.setInt(2, direct);
			ps.setDouble(3, money);
			ps.setInt(4, index);
			isInsert = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			JdbcUtil.release(conn, ps, rs);
		}
		return isInsert;
	}
//	public static void main(String[] args) 
//	{
//		Cost_add a = new Cost_add();
//		a.add("d", 2, 111);
//		a.add("daf", 22, 11.2);
//	}
}
